package com.wl.shop.web;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.Servlets;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wl.shop.entity.Category;
import com.wl.shop.entity.MyTemplate;
import com.wl.shop.model.DataTableResult;
import com.wl.shop.model.RestResponse;
import com.wl.shop.utils.Constants;
import com.wl.shop.utils.StringUtils;

@Controller
@RequestMapping("${adminPath}/template")
public class MyTemplateController extends BaseController{
	
	@GetMapping({"list",""})
	public String list(){
		return "cms/template/list";
	}
	
	@PostMapping("list")
	@ResponseBody
	public DataTableResult list(String aodata,ServletRequest request){
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		String name = (String)searchParams.get("name");
		String type = (String)searchParams.get("type");
		Map<String,Object>	map =Constants.convertToMap(aodata);
		int echo = (Integer) map.get("sEcho");
		DataTableResult result = new DataTableResult();
		result.setSEcho(echo);
		EntityWrapper<MyTemplate> wapper = new EntityWrapper<MyTemplate>();
		if(StringUtils.isNotBlank(name)){
			wapper.like("name", name);
		}
		if(StringUtils.isNotBlank(type)){
			wapper.eq("type", Integer.valueOf(type));
		}
		wapper.eq("is_del", false);
		int start = (Integer)map.get("iDisplayStart");
		int pageSize = (Integer)map.get("iDisplayLength");
		int pageNum = start/pageSize==0?1:(start/pageSize+1);
		if(pageSize<0){
			List<MyTemplate> list = templateService.selectList(wapper);
			result.setAaData(list);
			result.setITotalRecords(list.size());
			result.setITotalDisplayRecords(list.size());
		}else{
			Page<MyTemplate>  page = templateService.selectPage(new Page<MyTemplate>(pageNum,pageSize),wapper);
			result.setAaData(page.getRecords());
			result.setITotalRecords(page.getTotal());
			result.setITotalDisplayRecords(page.getTotal());
		}
		return result;
	}
	
	@GetMapping("add")
	public String add(){
		return "cms/template/add";
	}
	
	@ResponseBody
	@PostMapping("add")
	public RestResponse add(@RequestBody MyTemplate myTemplate){
		if(StringUtils.isBlank(myTemplate.getName())){
			return RestResponse.failure("模板名称不能为空");
		}
		if(myTemplate.getType() == null || ((myTemplate.getType() != 1) && (myTemplate.getType() !=2))){
			return RestResponse.failure("模板类型不正确");
		}
		if(StringUtils.isBlank(myTemplate.getContent())){
			return RestResponse.failure("模板内容不能为空");
		}
		templateService.saveOrUpdate(myTemplate);
		return RestResponse.success();
	}
	
	@GetMapping("edit")
	public String edit(String id,Model model){
		if(StringUtils.isBlank(id)){
			return "redirect:${adminPath}/template/list";
		}
		MyTemplate myTemplate = templateService.selectById(id);
		model.addAttribute("myTemplate", myTemplate);
		return "cms/template/edit";
	}
	
	@ResponseBody
	@PostMapping("edit")
	public RestResponse edit(@RequestBody MyTemplate myTemplate){
		if(StringUtils.isBlank(myTemplate.getId())){
			return RestResponse.failure("模板ID不能为空");
		}
		if(StringUtils.isBlank(myTemplate.getName())){
			return RestResponse.failure("模板名称不能为空");
		}
		if(myTemplate.getType() == null || ((myTemplate.getType() != 1) && (myTemplate.getType() !=2))){
			return RestResponse.failure("模板类型不正确");
		}
		if(StringUtils.isBlank(myTemplate.getContent())){
			return RestResponse.failure("模板内容不能为空");
		}
		templateService.saveOrUpdate(myTemplate);
		return RestResponse.success();
	}
	
	@PostMapping("delete")
	@ResponseBody
	public RestResponse delete(@RequestParam("ids[]")List<String> ids){
		if(ids == null || ids.size() == 0){
			return RestResponse.failure("删除ID不能为空");
		}
		
		EntityWrapper<Category> wrapper = new EntityWrapper<Category>();
		wrapper.eq("is_del", false).and().in("list_template", ids).or().in("content_template", ids);
		List<Category> categories = categoryService.selectList(wrapper);
		if(categories!=null && categories.size()>0){
			return RestResponse.failure("该模板已被使用,不能删除");
		}
		List<MyTemplate> list = templateService.selectBatchIds(ids);
		for (MyTemplate myTemplate : list) {
			myTemplate.setIsDel(true);
			myTemplate.preUpdate();
		}
		templateService.updateBatchById(list);
		return RestResponse.success();
	}
}
